package com.ssy.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.crypto.SecureUtil;
import com.ssy.common.CodeMsg;
import com.ssy.common.PageInfo;
import com.ssy.common.Result;
import com.ssy.dao.UserDao;
import com.ssy.entity.User;
import com.ssy.service.UserService;

import java.util.Date;
import java.util.Map;

/**
 * UserServiceImpl
 *
 * @author ssy
 * @date 2020/11/12
 **/
public class UserServiceImpl implements UserService {

    private static final String DEFALUT_PASSWORD = "123456";

    UserDao userDao = new UserDao();

    @Override
    public Result updatePwd(String password, String newPassword, int uId) {
        String md5Pwd = SecureUtil.md5(password);
        String newMd5Pwd = SecureUtil.md5(newPassword);
        User user = this.userDao.selectUserByUid(uId);
        if (!user.getUpwd().equalsIgnoreCase(md5Pwd)) {
            return new Result(CodeMsg.USER_UPDATE_PWD_ERROR);
        }
        boolean flag = this.userDao.updateUserPwd(newMd5Pwd, uId);
        return flag ? new Result(CodeMsg.UPDATE_SUCCES) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result updateUser(String uname, int uId, int utype) {
        User user = this.userDao.selectUserByUid(uId);

        //验证用户名重复
        if (!user.getUname().equalsIgnoreCase(uname)) {
            User user2 = this.userDao.selectOneUser(uname);
            if (user2 != null) {
                return new Result(CodeMsg.USER_ALREADY_EXISTS);
            }
        }

        boolean flag = this.userDao.updateUsers(uname, utype, DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), uId);
        return flag ? new Result(CodeMsg.UPDATE_SUCCES) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result login(String userName, String password) {
        String md5Pwd = SecureUtil.md5(password);
        User user = this.userDao.queryByUserNameAndPwd(userName, md5Pwd);
        if (null == user) {
            return new Result(CodeMsg.USER_LOGIN_ERROR);
        }
        return new Result(user);
    }

    @Override
    public Result deleteOneUser(String id) {
        boolean flag = this.userDao.deleteUser(id);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result deteleUser(String[] ids) {
        boolean flag = this.userDao.batchDeleteUsers(ids);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result restUserPassword(String id) {
        String md5Pwd = SecureUtil.md5(DEFALUT_PASSWORD);
        boolean flag = this.userDao.restUserPwd(id, md5Pwd);
        return flag ? new Result(CodeMsg.REST_USER_PWD_SUCCES) : new Result(CodeMsg.REST_USER_PWD__ERROR);
    }

    @Override
    public Result addUser(String uname, int utype) {
        User user = this.userDao.selectOneUser(uname);
        if (user != null) {
            return new Result(CodeMsg.USER_ALREADY_EXISTS);
        }
        String md5Pwd = SecureUtil.md5(DEFALUT_PASSWORD);
        boolean flag = userDao.insertUser(uname, utype, md5Pwd, DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        return flag ? new Result(CodeMsg.ADD_SUCCES) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result queryPage(Map<String, Object> param, String page, String limit) {
        Integer pageNo = NumberUtil.isNumber(page) ? Integer.parseInt(page) : 1;
        Integer limitNo = NumberUtil.isNumber(limit) ? Integer.parseInt(limit) : 10;
        PageInfo<User> userPageInfo = userDao.selectPage(param, pageNo, limitNo);
        return new Result(userPageInfo);
    }

}
